package mybatis.test;

import mybatis.dao.GradeDao;
import mybatis.pojo.Grade;
import mybatis.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @Author 汤舒青
 * @Version 1.0
 */


public class TestGrade {
    /**
     * 增
     */
    @Test
    public void add() {
        SqlSession s = MybatisUtil.get();
        Grade grade = new Grade("家里蹲");
        GradeDao mapper = s.getMapper(GradeDao.class);
        int add = mapper.add(grade);
        System.out.println(add);
        System.out.println("增加的年级编号" + grade.getId());
        MybatisUtil.close(s);
    }

    /**
     * 删
     */
    @Test
    public void deleteGrade() {
        SqlSession s = MybatisUtil.get();
        GradeDao mapper = s.getMapper(GradeDao.class);
        int i = mapper.delete(20);
        System.out.println(i == 1 ? "删除成功" : "删除失败");
        MybatisUtil.close(s);

    }

    /**
     * 查
     */
    @Test
    public void findGrade() {
        SqlSession s = MybatisUtil.get();
        GradeDao mapper = s.getMapper(GradeDao.class);
        List<Grade> list = mapper.findAll();
        list.forEach(System.out::println);
        MybatisUtil.close(s);
    }

    /**
     * 分页
     */
    @Test
    public void findLimit() {
        SqlSession s = MybatisUtil.get();
        GradeDao mapper = s.getMapper(GradeDao.class);
        List<Grade> allLimit = mapper.findAllLimit(1, 5);
        for (Grade grade : allLimit) {
            System.out.println(grade);
        }

    }

    /**
     * 改
     */
    @Test
    public void updateGrade() {
        SqlSession s = MybatisUtil.get();
        GradeDao mapper = s.getMapper(GradeDao.class);
        Grade grade = new Grade(2, "北大清华");
        int update = mapper.update(grade);
        System.out.println(update == 1 ? "更新成功" : "更新失败");
        MybatisUtil.close(s);
    }

}
